home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d927.lha / Telnet / src / getopt.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  3KB  |  104 lines

  1. /*
  2.  * Copyright (c) 1987 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)getopt.c    4.12 (Berkeley) 6/1/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. /*
  28.  * get option letter from argument vector
  29.  */
  30. int    opterr = 1,        /* if error message should be printed */
  31.     optind = 1,        /* index into parent argv vector */
  32.     optopt;            /* character checked for validity */
  33. char    *optarg;        /* argument associated with option */
  34.  
  35. #define    BADCH    (int)'?'
  36. #define    EMSG    ""
  37.  
  38. getopt(nargc, nargv, ostr)
  39.     int nargc;
  40.     char **nargv, *ostr;
  41. {
  42.     static char *place = EMSG;        /* option letter processing */
  43.     register char *oli;            /* option letter list index */
  44.     char *p;
  45.  
  46.     if (!*place) {                /* update scanning pointer */
  47.         if (optind >= nargc || *(place = nargv[optind]) != '-') {
  48.             place = EMSG;
  49.             return(EOF);
  50.         }
  51.         if (place[1] && *++place == '-') {    /* found "--" */
  52.             ++optind;
  53.             place = EMSG;
  54.             return(EOF);
  55.         }
  56.     }                    /* option letter okay? */
  57.     if ((optopt = (int)*place++) == (int)':' ||
  58.         !(oli = index(ostr, optopt))) {
  59.         /*
  60.          * if the user didn't specify '-' as an option,
  61.          * assume it means EOF.
  62.          */
  63.         if (optopt == (int)'-')
  64.             return(EOF);
  65.         if (!*place)
  66.             ++optind;
  67.         if (opterr) {
  68.             if (!(p = rindex(*nargv, '/')))
  69.                 p = *nargv;
  70.             else
  71.                 ++p;
  72.             (void)fprintf(stderr, "%s: illegal option -- %c\n",
  73.                 p, optopt);
  74.         }
  75.         return(BADCH);
  76.     }
  77.     if (*++oli != ':') {            /* don't need argument */
  78.         optarg = NULL;
  79.         if (!*place)
  80.             ++optind;
  81.     }
  82.     else {                    /* need an argument */
  83.         if (*place)            /* no white space */
  84.             optarg = place;
  85.         else if (nargc <= ++optind) {    /* no arg */
  86.             place = EMSG;
  87.             if (!(p = rindex(*nargv, '/')))
  88.                 p = *nargv;
  89.             else
  90.                 ++p;
  91.             if (opterr)
  92.                 (void)fprintf(stderr,
  93.                     "%s: option requires an argument -- %c\n",
  94.                     p, optopt);
  95.             return(BADCH);
  96.         }
  97.          else                /* white space */
  98.             optarg = nargv[optind];
  99.         place = EMSG;
  100.         ++optind;
  101.     }
  102.     return(optopt);                /* dump back option letter */
  103. }
  104.